#coding=utf-8
#Boa:Dialog:DlgEventEditor

import wx
import wx.lib.masked.timectrl
import wx.lib.masked.textctrl
import wx.gizmos
from product import CDataEvent

def create(parent):
    return DlgEventEditor(parent)

[wxID_DLGEVENTEDITOR, wxID_DLGEVENTEDITORBTNCANCEL, wxID_DLGEVENTEDITORBTNOK, 
 wxID_DLGEVENTEDITORCBSTART, wxID_DLGEVENTEDITORDATESTART, 
 wxID_DLGEVENTEDITORTALERT, wxID_DLGEVENTEDITORTCCONTENT, 
 wxID_DLGEVENTEDITORTCONTENT, wxID_DLGEVENTEDITORTCTITLE, 
 wxID_DLGEVENTEDITORTDATE, wxID_DLGEVENTEDITORTIMESTART, 
 wxID_DLGEVENTEDITORTTITLE, 
] = [wx.NewId() for _init_ctrls in range(12)]

class DlgEventEditor(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGEVENTEDITOR, name=u'DlgEventEditor',
              parent=prnt, pos=wx.Point(404, 261), size=wx.Size(511, 382),
              style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u8bbe\u7f6e\u5f00\u673a\u4e8b\u4ef6\u63d0\u9192')
        self.SetClientSize(wx.Size(503, 348))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))

        self.tTitle = wx.StaticText(id=wxID_DLGEVENTEDITORTTITLE,
              label=u'\u4e3b\u9898\uff1a', name=u'tTitle', parent=self,
              pos=wx.Point(32, 48), size=wx.Size(56, 14), style=0)

        self.tcTitle = wx.TextCtrl(id=wxID_DLGEVENTEDITORTCTITLE,
              name=u'tcTitle', parent=self, pos=wx.Point(96, 40),
              size=wx.Size(376, 22), style=0, value=u'')

        self.tContent = wx.StaticText(id=wxID_DLGEVENTEDITORTCONTENT,
              label=u'\u5185\u5bb9\uff1a', name=u'tContent', parent=self,
              pos=wx.Point(32, 88), size=wx.Size(56, 14), style=0)

        self.tcContent = wx.TextCtrl(id=wxID_DLGEVENTEDITORTCCONTENT,
              name=u'tcContent', parent=self, pos=wx.Point(96, 88),
              size=wx.Size(376, 96), style=wx.TE_MULTILINE, value=u'')

        self.tAlert = wx.StaticText(id=wxID_DLGEVENTEDITORTALERT,
              label=u'\u63d0\u9192\u8bbe\u7f6e\uff1a', name=u'tAlert',
              parent=self, pos=wx.Point(32, 208), size=wx.Size(96, 14),
              style=0)
        self.tAlert.Show(False)

        self.cbStart = wx.CheckBox(id=wxID_DLGEVENTEDITORCBSTART,
              label=u'\u8f6f\u4ef6\u542f\u52a8\u65f6\u81ea\u52a8\u63d0\u9192',
              name=u'cbStart', parent=self, pos=wx.Point(160, 208),
              size=wx.Size(136, 14), style=0)
        self.cbStart.SetValue(True)
        self.cbStart.Show(False)

        self.btnOK = wx.Button(id=wxID_DLGEVENTEDITORBTNOK,
              label=u'\u786e\u5b9a', name=u'btnOK', parent=self,
              pos=wx.Point(120, 288), size=wx.Size(75, 24), style=0)
        self.btnOK.Bind(wx.EVT_BUTTON, self.OnBtnOKButton,
              id=wxID_DLGEVENTEDITORBTNOK)

        self.btnCancel = wx.Button(id=wxID_DLGEVENTEDITORBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(264, 288), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGEVENTEDITORBTNCANCEL)

        self.tDate = wx.StaticText(id=wxID_DLGEVENTEDITORTDATE,
              label=u'\u63d0\u9192\u5f00\u59cb\u65f6\u95f4\uff1a',
              name=u'tDate', parent=self, pos=wx.Point(32, 240),
              size=wx.Size(120, 14), style=0)

        self.dateStart = wx.DatePickerCtrl(id=wxID_DLGEVENTEDITORDATESTART,
              name=u'dateStart', parent=self, pos=wx.Point(160, 232),
              size=wx.Size(120, 22), style=wx.DP_SHOWCENTURY)

        self.timeStart = wx.lib.masked.timectrl.TimeCtrl(display_seconds=False,
              fmt24hr=True, id=wxID_DLGEVENTEDITORTIMESTART, name=u'timeStart',
              oob_color=wx.NamedColour('Yellow'), parent=self, pos=wx.Point(288,
              232), size=wx.Size(53, 22), style=0, useFixedWidthFont=False,
              value=u'00:00:00')

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.m_event = None
        

##    def setData(self, title='', content='', date=''):
##        self.tcTitle.SetValue(title)
##        self.tcContent.SetValue(content)
##        
##        if (date == ''):
##            self.dateStart.SetValue(wx.DateTime.Today())
    
    def setData(self, obj):
        self.m_event = obj
        self.tcTitle.SetValue(obj.getValue(obj.iTitle))
        self.tcContent.SetValue(obj.getValue(obj.iContent))
        #self.dateStart.SetValue(obj.getValue(obj.iStartDate))
        date = obj.getValue(obj.iStartDate)
        self.dateStart.SetValue(wx.DateTimeFromDMY(date.day, date.month-1, date.year,0,0,0))
        self.timeStart.SetValue('%d:%d' %(date.hour,date.minute))
        
    def OnBtnOKButton(self, event):
        title = self.tcTitle.GetValue()
        content = self.tcContent.GetValue()
        date = self.dateStart.GetValue().Format('%Y-%m-%d') + ' ' + self.timeStart.GetValue()
        if (self.cbStart.IsChecked()):
            bootAlarm = 'T'
        else:
            bootAlarm = 'F'
        
        data = {CDataEvent.iTitle:title,
                CDataEvent.iContent:content,
                CDataEvent.ibootAlarm:bootAlarm,
                CDataEvent.iStartDate:date}
        if (self.m_event is not None):
            self.m_event.setMulFieldVal(data)
            self.m_event.update()
        else:
            obj = CDataEvent.factory()
            obj.setMulFieldVal(data)
            obj.insert()
            self.m_event = obj
        
        self.EndModal(wx.ID_OK)
        
    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)
